package com.szzg10.mina2.chapter9;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class ImageResponseEncoder extends ProtocolEncoderAdapter {

	@Override
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
			throws Exception {
		
		ImageResponse imageResponse = (ImageResponse)message;
		BufferedImage image1 = imageResponse.getImage1();
		BufferedImage image2 = imageResponse.getImage2();
		
		byte[] byte1 = getBytes(image1);
		byte[] byte2 = getBytes(image2);
		
		int capacity = byte1.length + byte2.length + 8;
		IoBuffer buffer = IoBuffer.allocate(capacity, false);
		buffer.setAutoExpand(true);
		
		buffer.putInt(byte1.length);
		buffer.put(byte1);
		buffer.putInt(byte2.length);
		buffer.put(byte2);
		buffer.flip();
		
		out.write(buffer);
	}
	
	private byte[] getBytes(BufferedImage image) throws IOException {
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "jpg", baos);
		
		return baos.toByteArray();
	}
}
